package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.dao.UpmpUserLoginLogMapper;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserLoginLog;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserLoginLogService;
import cc.rengu.redp.common.utils.AddressUtil;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.HttpContextUtil;
import cc.rengu.redp.common.utils.IPUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author System
 */
@Slf4j
@Service
public class UpmpUserLoginLogServiceImpl extends ServiceImpl<UpmpUserLoginLogMapper, UpmpUserLoginLog> implements IUpmpUserLoginLogService {

    @Override
    public void saveLoginLog(UpmpUserLoginLog userLoginLog) {

        userLoginLog.setLoginonTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN));
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        String ip = IPUtil.getIpAddr(request);
        userLoginLog.setClientAddress(request.getRemoteAddr());
        userLoginLog.setClientHost(request.getRemoteHost());
        userLoginLog.setClientPort(request.getRemotePort());
        userLoginLog.setClientUserAgent(request.getHeader("User-Agent"));
        userLoginLog.setClientOriginIp(IPUtil.getIpAddr(request));
        userLoginLog.setClientLocation(AddressUtil.getCityInfo(ip));
        this.save(userLoginLog);
    }
}
